<% layout('/layouts/default.html', {title: '送货通知管理', libs: ['validate','dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa icon-note"></i> ${text(stSendNoticeBill.isNewRecord ? '新增送货通知' : '编辑送货通知')}
			</div>
			<div class="box-tools pull-right">
				<a href="#" class="btn btn-default" id="btn-src" title="${text('源单')}"><i class="fa fa-filter"></i> ${text('源单')}</a>
				<script>
					$("#btn-src").click(function (){
						var custom = $("#custom").val();
						if(""==custom.trim()){
							js.showMessage("没选择客户");
						}else{
							$("#btn-src-0Div").attr("data-url", "${ctx}/st/stSaleBill/forChoose?custom=" + custom);
							$("#btn-src-0Button").click();
						}
					});

					function chooseSale(id, act, index, layero, selectData){
						var selectLength = selectData.length;
						var idArr = [];
						for (var i = 0; i < selectLength; i++) {
							idArr.push(selectData[i].id);
						}
						$(".del").click();
						$.ajax({
							url:"${ctx}/st/stSaleBill/listDetail",
							dataType:'json',
							type:'POST',
							data:{
								//请求参数
								id: idArr.join(",")
							},
							success:function(result){
								console.info(result);
								for(var i=0;i<result.length;i++){
									$("#stSendNoticeBillDetailDataGridAddRowBtn").click();
									var item = result[i];
									var materialName = $(($(($("#stSendNoticeBillDetailDataGrid").find("tr"))[1 /*+ t_size_1 + formula_list.length + pre_mix*/]).find('td'))[3]);
									materialName.find(".form-control").val(item.productName).trigger('change');
									materialName.find("input:hidden").val(item.productName).attr("material-id",item.productName);
									var number = materialName.next();
									number.find(".form-control").val(item.number);
									var unit = number.next();
									unit.find(".form-control").val(item.unit);
									var primaryId = unit.next();
									primaryId.find(".form-control").val(item.id);
								}
							}
						});

					}
				</script>
				<div class="hide">
					<#form:listselect id="btn-src-0" title="销售单"

					url="${ctx}/st/stPurchaseBill/list" allowClear="false"
					checkbox="false"  callbackFuncName="chooseSale"/>
				</div>
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
			</div>
		</div>
		<#form:form id="inputForm" model="${stSendNoticeBill}" action="${ctx}/st/stSendNoticeBill/save" method="post" class="form-horizontal">
			<div class="box-body">
				<div class="form-unit">${text('基本信息')}</div>
				<#form:hidden path="id"/>
				<div class="row">
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('单据号')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="billNo" maxlength="20" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('单据日期')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="billDate" readonly="true" maxlength="20" class="form-control laydate"
									dataFormat="datetime" data-type="datetime" data-format="yyyy-MM-dd HH:mm"/>
							</div>
						</div>
					</div>
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('销售单号')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="saleBillNo" maxlength="20" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('物料编码')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="materialCode" maxlength="20" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('物料名称')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="materialName" maxlength="36" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('客户')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="custom" items="${customList}"  itemLabel="companyFullName" itemValue="id" blankOption="true" class="form-control" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('业务员')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="dutyMan" maxlength="5" class="form-control"/>
							</div>
						</div>
					</div>
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('业务员编码')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="dutyManCode" maxlength="20" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('收货地址')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="address" maxlength="40" class="form-control"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('收货人')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="consignee" maxlength="5" class="form-control"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('电话')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="phone" maxlength="20" class="form-control"/>
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2" title="">
								<span class="required hide">*</span> ${text('备注')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-10">
								<#form:textarea path="remarks" rows="4" maxlength="500" class="form-control"/>
							</div>
						</div>
					</div>
				</div>
				<h4 class="form-unit">${text('送货通知单明细')}</h4>
				<div class="ml10 mr10">
					<table id="stSendNoticeBillDetailDataGrid"></table>
					<% if (hasPermi('st:stSendNoticeBill:edit')){ %>
						<a href="#" id="stSendNoticeBillDetailDataGridAddRowBtn" class="btn btn-primary btn-sm mt10 mb10" style="display: none"><i class="fa fa-plus"></i> ${text('增行')}</a>
					<% } %>
				</div>
			</div>
			<div class="box-footer">
				<div class="row">
					<div class="col-sm-offset-2 col-sm-10">
						<% if (hasPermi('st:stSendNoticeBill:edit')){ %>
							<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('保 存')}</button>&nbsp;
						<% } %>
						<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
					</div>
				</div>
			</div>
		</#form:form>
	</div>
</div>
<% } %>
<style>
	.del{

	}
</style>
<script>
//初始化送货通知单明细DataGrid对象
$("#stSendNoticeBillDetailDataGrid").dataGrid({

	data: ${toJson(stSendNoticeBill.stSendNoticeBillDetailList)},
	datatype: "local", // 设置本地数据
	autoGridHeight: function(){return 'auto'}, // 设置自动高度
	
	// 设置数据表格列
	columnModel: [
		{header:'状态', name:'status', editable:true, hidden:true},
		{header:'主键', name:'id', editable:true, hidden:true},
		{header:'${text("物料")}', name:'materialName', width:150,
			editable:true, edittype:'select', editoptions:{'class':'form-control',
				items: $.merge([{productName:'&nbsp;',id:''}], ${productList}),
				itemLabel: 'productName', itemValue: 'id', dataInit: function(element){
					$(element).select2().on("change",function(){$(this).resetValid()});
					element.name="ignore_materialName";
					$(element).attr("disabled",true);
					$(element).after("<input name='materialName' material-id='' value='"+element.value+"' hidden>");
				}
			}
		},
		{header:'${text("数量")}', name:'number', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'8', 'class':'form-control digits'}},
		{header:'${text("单位")}', name:'unit', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'36', 'class':'form-control',readonly:true}},
		{header:'${text("单位")}', name:'primaryId', width:150, editable:true, edittype:'text', hidden:true,editoptions:{'maxlength':'36', 'class':'form-control',readonly:true}},
		{header:'${text("备注")}', name:'remarks', width:150, editable:true, edittype:'textarea', editoptions:{'maxlength':'500', 'class':'form-control', 'rows':'1'}},
		{header:'${text("父 id")}', name:'parentId.id', editable:true, hidden:true},
		{header:'${text("操作")}', name:'actions', width:80, sortable:false, fixed:true, formatter: function(val, obj, row, act){
			var actions = [];
			if (val == 'new'){
				// actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#stSendNoticeBillDetailDataGrid\').dataGrid(\'delRowData\',\''+obj.rowId+'\')});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
				actions.push('<a href="#" class="del" onclick="$(\'#stSendNoticeBillDetailDataGrid\').dataGrid(\'delRowData\',\''+obj.rowId+'\')"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}else{
				// actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#stSendNoticeBillDetailDataGrid\').dataGrid(\'setRowData\',\''+obj.rowId+'\',null,{display:\'none\'});$(\'#'+obj.rowId+'_status\').val(\''+Global.STATUS_DELETE+'\');});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
				actions.push('<a href="#" class="del" onclick="$(\'#stSendNoticeBillDetailDataGrid\').dataGrid(\'setRowData\',\''+obj.rowId+'\',null,{display:\'none\'});$(\'#'+obj.rowId+'_status\').val(\''+Global.STATUS_DELETE+'\');"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}
			return actions.join('');
		}, editoptions: {defaultValue: 'new'}}
	],
	
	// 编辑表格参数
	editGrid: true,				// 是否是编辑表格
	editGridInitRowNum: 1,		// 编辑表格的初始化新增行数
	editGridAddRowBtn: $('#stSendNoticeBillDetailDataGridAddRowBtn'),	// 子表增行按钮
	editGridAddRowInitData: {id: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据
	
	// 编辑表格的提交数据参数
	editGridInputFormListName: 'stSendNoticeBillDetailList', // 提交的数据列表名
	editGridInputFormListAttrs: 'status,id,materialName,number,unit,remarks,parentId.id,primaryId', // 提交数据列表的属性字段
	
	// 加载成功后执行事件
	ajaxSuccess: function(data){
		
	}
});
</script>
<script>
$("#inputForm").validate({
	submitHandler: function(form){
		js.ajaxSubmitForm($(form), function(data){
			js.showMessage(data.message);
			if(data.result == Global.TRUE){
				js.closeCurrentTabPage(function(contentWindow){
					contentWindow.page();
				});
			}
		}, "json");
    }
});
</script>